

#include "user_io.h"
#include "log.h"
#include "main.h"
#include "menu.h"
#include "user_motor_drv.h"
#include "gd32l23x.h"
#include "input_check.h"

extern uint8_t cycle_dir;


uint8_t get_card_det_stats(void)
{
    static uint32_t pre_adc;
    static uint32_t last_timer;
    static uint8_t pre_status = 0;

    if(last_timer == system_1ms_cnt) {
        return pre_status;
    }

    last_timer = system_1ms_cnt;

    if(pre_adc != adc_buf[S_HW_ADC]) {
        hw_send = ((uint32_t)(adc_buf[S_HW_ADC] * 120 / adc_buf[INTER_ADC]));
        pre_adc = adc_buf[S_HW_ADC];
    }

#ifdef LINURM_DEBUG_SEND_CHECK_FAN
#if 1

    if((hw_send < 80) && (pre_status == 1)) {
        pre_status = 0;

    } else if((hw_send > 120) && (pre_status == 0)) {
        pre_status = 1;
    }

    return pre_status;
    //pre_status = 0;
#else

    //if(gpio_input_bit_get(GPIOC, GPIO_PIN_1)) {
    if(hw_send < 120) {
        pre_status = 0;
        return 0;

    } else {
        pre_status = 1;
        return 1;
    }

#endif
#else

    //if(gpio_input_bit_get(GPIOC, GPIO_PIN_1)) {
    if(hw_send < 120) {
        return 1;

    } else {
        return 0;
    }

#endif
}


#if 1
uint8_t get_check_rotate(void)
{
    uint8_t t = 0;
    static uint32_t pre_adc;
    static uint32_t pre_adc2;
    static uint32_t last_timer;
    static uint8_t pre_status = 0xF;
    static uint8_t pre_status2 = 0xF;

    if(last_timer == system_1ms_cnt) {
        if(cycle_dir) {
            return pre_status;

        } else {
            return pre_status;
        }
    }

    last_timer = system_1ms_cnt;

    //if(cycle_dir) {
    if(pre_adc != adc_buf[R_HW_ADC]) {
        hw_rotate = ((uint32_t)(adc_buf[R_HW_ADC] * 120 / adc_buf[INTER_ADC]));
        pre_adc = adc_buf[R_HW_ADC];
    }

    //}
#if 1

    if((hw_rotate < 80) && (pre_status != 4)) {
        pre_status = 4;

    } else if((hw_rotate > 120) && (pre_status != 0)) {
        pre_status = 0;
    }

    if(cycle_dir) {
        if(pre_status == 4) {
            t |= 4;
        }

    } else {
        if(pre_status == 4) {
            t |= 4;
        }
    }

#else

    //if(gpio_input_bit_get(GPIOC, GPIO_PIN_5)) {
    if(hw_rotate < 120) {
        pre_status = 4;
        t |= 4;

    } else {
        pre_status = 0;
    }

#endif
    return t;
}
uint8_t get_cycle_det_stats(void)
{
    static uint32_t pre_adc;
#ifdef LINURM_DEBUG_16_DEC_STOP
    static uint32_t last_timer;
    static uint8_t pre_status = 0xF;

    if(last_timer == system_1ms_cnt) {
        if(cycle_dir) {
            return pre_status;

        } else {
            return pre_status;
        }
    }

    last_timer = system_1ms_cnt;

    if(pre_adc != adc_buf[R_HW_ADC]) {
        hw_rotate = ((uint32_t)(adc_buf[R_HW_ADC] * 120 / adc_buf[INTER_ADC]));
        pre_adc = adc_buf[R_HW_ADC];
    }

    if((hw_rotate < 80) && (pre_status != 1)) {
        pre_status = 1;

    } else if((hw_rotate > 120) && (pre_status != 0)) {
        pre_status = 0;
    }

    //if(GPIO_ReadInputDataBit(GPIOB, GPIO_PIN_15)) {
    //if(gpio_input_bit_get(GPIOC, GPIO_PIN_5)) {
    if(cycle_dir) {
        return pre_status;

    } else {
        return pre_status;
    }

#if 1
    /*if((hw_rotate < 80) && (pre_status != 1)) {
        pre_status = 1;

    } else if((hw_rotate > 120) && (pre_status != 0)) {
        pre_status = 0;
    }*/
    //pre_status = 0;
#else

    if(hw_rotate < 120) {
        pre_status = 1;
        return 1;
    }

    pre_status = 0;
#endif
#else
    //if(GPIO_ReadInputDataBit(GPIOB, GPIO_PIN_15)) {
    //if(gpio_input_bit_get(GPIOC, GPIO_PIN_5)) {
#endif
    //return 1;
    //}
    // }
    //} else {
    return 0;
    //}
}

uint8_t get_cycle_half_det_stats(void)
{
    //if(GPIO_ReadInputDataBit(GPIOB, GPIO_PIN_12)) {
    //if(GPIO_ReadInputDataBit(GPIOB, GPIO_PIN_15)) {
    //if(GPIO_ReadInputDataBit(GPIOB, GPIO_PIN_12)) {
    return 1;
    //}
    // }
    //} else {
    return 0;
    //}
}

#else
uint8_t get_check_rotate(void)
{
    uint8_t t = 0;
    static uint32_t pre_adc;
    static uint32_t pre_adc2;
    static uint32_t last_timer;
    static uint8_t pre_status = 0xF;
    static uint8_t pre_status2 = 0xF;

    if(last_timer == system_1ms_cnt) {
        if(cycle_dir) {
            return pre_status2;

        } else {
            return pre_status;
        }
    }

    last_timer = system_1ms_cnt;

    //if(cycle_dir) {
    if(pre_adc != adc_buf[1]) {
        hw_rotate = ((uint32_t)(adc_buf[1] * 120 / adc_buf[9]));
        pre_adc = adc_buf[1];
    }

    //} else {
    if(pre_adc2 != adc_buf[3]) {
        hw_rotate2 = ((uint32_t)(adc_buf[3] * 120 / adc_buf[9]));
        pre_adc2 = adc_buf[3];
    }

    //}
#if 1

    if(cycle_dir) {
        if((hw_rotate2 < 80) && (pre_status2 != 4)) {
            pre_status2 = 4;

        } else if((hw_rotate2 > 120) && (pre_status2 != 0)) {
            pre_status2 = 0;
        }

        if(pre_status2 == 4) {
            t |= 4;
        }

    } else {
        if((hw_rotate < 80) && (pre_status != 4)) {
            pre_status = 4;

        } else if((hw_rotate > 120) && (pre_status != 0)) {
            pre_status = 0;
        }

        if(pre_status == 4) {
            t |= 4;
        }
    }

#else

    //if(gpio_input_bit_get(GPIOC, GPIO_PIN_5)) {
    if(hw_rotate < 120) {
        pre_status = 4;
        t |= 4;

    } else {
        pre_status = 0;
    }

#endif
    /*if(GPIO_ReadInputDataBit(GPIOB, GPIO_PIN_15)) {
        t |= 2;
    }

    if(GPIO_ReadInputDataBit(GPIOB, GPIO_PIN_12)) {
        t |= 1;
    }*/
    /*if(GPIO_ReadInputDataBit(GPIOD, GPIO_PIN_2)) {
        t |= 1;
    }*/
    return t;
}
uint8_t get_cycle_det_stats(void)
{
    static uint32_t pre_adc;
    static uint32_t pre_adc2;
#ifdef LINURM_DEBUG_16_DEC_STOP
    static uint32_t last_timer;
    static uint8_t pre_status = 0xF;
    static uint8_t pre_status2 = 0xF;

    if(last_timer == system_1ms_cnt) {
        if(cycle_dir) {
            return pre_status2;

        } else {
            return pre_status;
        }
    }

    last_timer = system_1ms_cnt;

    if(pre_adc != adc_buf[1]) {
        hw_rotate = ((uint32_t)(adc_buf[1] * 120 / adc_buf[9]));
        pre_adc = adc_buf[1];
    }

    if(pre_adc2 != adc_buf[3]) {
        hw_rotate2 = ((uint32_t)(adc_buf[3] * 120 / adc_buf[9]));
        pre_adc2 = adc_buf[3];
    }

    //if(GPIO_ReadInputDataBit(GPIOB, GPIO_PIN_15)) {
    //if(gpio_input_bit_get(GPIOC, GPIO_PIN_5)) {
    if(cycle_dir) {
        if((hw_rotate2 < 80) && (pre_status2 != 1)) {
            pre_status2 = 1;

        } else if((hw_rotate2 > 120) && (pre_status2 != 0)) {
            pre_status2 = 0;
        }

        return pre_status2;

    } else {
        if((hw_rotate < 80) && (pre_status != 1)) {
            pre_status = 1;

        } else if((hw_rotate > 120) && (pre_status != 0)) {
            pre_status = 0;
        }

        return pre_status;
    }

#if 1
    /*if((hw_rotate < 80) && (pre_status != 1)) {
        pre_status = 1;

    } else if((hw_rotate > 120) && (pre_status != 0)) {
        pre_status = 0;
    }*/
    //pre_status = 0;
#else

    if(hw_rotate < 120) {
        pre_status = 1;
        return 1;
    }

    pre_status = 0;
#endif
#else
    //if(GPIO_ReadInputDataBit(GPIOB, GPIO_PIN_15)) {
    //if(gpio_input_bit_get(GPIOC, GPIO_PIN_5)) {
#endif
    //return 1;
    //}
    // }
    //} else {
    return 0;
    //}
}

uint8_t get_cycle_half_det_stats(void)
{
    //if(GPIO_ReadInputDataBit(GPIOB, GPIO_PIN_12)) {
    //if(GPIO_ReadInputDataBit(GPIOB, GPIO_PIN_15)) {
    //if(GPIO_ReadInputDataBit(GPIOB, GPIO_PIN_12)) {
    return 1;
    //}
    // }
    //} else {
    return 0;
    //}
}
#endif


